package sessionBean;

import java.util.List;

import interfaceBeanRemote.sessionAvancementRemote;

import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

import entityBean.Avancement;
import entityBean.Investisseur;
import entityBean.Startup;

/**
 * Session Bean implementation class sessionAvancement
 */
@Stateless
public class sessionAvancement implements sessionAvancementRemote {

	@PersistenceContext(unitName = "EJB3_Entity")
	private EntityManager em;

	@PostConstruct
	public void init() {
		System.out.println("calling init method");
	}

	@PreDestroy
	public void cleanup() {
		System.out.println("calling cleanup method");
	}

	/**
	 * Default constructor.
	 */
	public sessionAvancement() {
		// TODO Auto-generated constructor stub
	}
	
	@Override
	public void addAvancement(Avancement avancement) {
		// TODO Auto-generated method stub
		em.persist(avancement);
	}
	
	@Override
	public Avancement findAvancement(int id) {
		// TODO Auto-generated method stub
		return em.find(Avancement.class, id);
	}
	
	@Override
	public void removeAvancementById(int id) {
		em.remove(em.find(Avancement.class, id));

	}

	@Override
	public void updateAvancement(Avancement av) {
		em.merge(av);
	}
	
	@SuppressWarnings("unchecked")
	@Override
	public List<Avancement> getAvancementListWithStartUp(int idclub) {
		// TODO Auto-generated method stub
		Query query = em
				.createQuery("SELECT a FROM Avancement AS a right join fetch a.startup WHERE a.club.idclub = " + idclub
						+ " ORDER BY a.idavancement ASC");
		return query.getResultList();
	}
	
	@SuppressWarnings("unchecked")
	@Override
	public List<Avancement> getAvancementListWithClub(int idStartUp) {
		// TODO Auto-generated method stub
		Query query = em
				.createQuery("SELECT a FROM Avancement AS a inner join fetch a.club WHERE a.startup.idStartUp = " + idStartUp
						+ " ORDER BY a.idavancement ASC");
		return query.getResultList();
	}
	
	@Override
	public void refresh(Avancement av) {
		// TODO Auto-generated method stub
		em.refresh(av);
	}
	
	@Override
	public void flush() {
		// TODO Auto-generated method stub
		em.flush();
	}


}
